package name.jiaosha.air.company.controller;

import java.util.ArrayList;
import java.util.List;

import name.jiaosha.air.AppConstants;
import name.jiaosha.air.company.domain.CarInfo;
import name.jiaosha.air.company.service.ICompanyService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ali8ba.common.PageInfo;

@Controller
@RequestMapping("/admin/com")
public class CarInfoController {

	@Autowired
	private ICompanyService companyService;

	@RequestMapping("/carList")
	public ModelAndView getMyCarInfoList(){

		PageInfo<CarInfo> pageInfo = null;
		if (!AppConstants.DEBUG) {
			List<CarInfo> list = new ArrayList<CarInfo>();
			for (int i = 1; i <= 20; i++) {

				CarInfo carInfo = new CarInfo();
				carInfo.setId(i);
				carInfo.setCarCode("浙A234" + i);
				carInfo.setName("标志307" + i);
				list.add(carInfo);
			}

		} else {

			pageInfo = companyService.getCarListByComId(AppConstants.comId, 1,
					AppConstants.DEFAULT_SIZE);
		}

		ModelMap model = new ModelMap();
		model.addAttribute("root", pageInfo.getItemList());
		model.addAttribute("totalProperty", pageInfo.getTotalProperty());
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("createCar")
	public ModelAndView save(@RequestParam String carCode,
			@RequestParam String name) {

		if (!AppConstants.DEBUG) {

		} else {

			CarInfo carInfo = new CarInfo();
			carInfo.setComId(AppConstants.comId);
			carInfo.setCarCode(carCode.trim());
			carInfo.setName(name.trim());
			companyService.createCarInfo(carInfo);
		}

		ModelMap model = new ModelMap();
		model.addAttribute("success", "true");
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/updateCar")
	public ModelAndView update(@RequestParam int id,
			@RequestParam String carCode, @RequestParam String name) {

		if (!AppConstants.DEBUG) {

		} else {

			CarInfo carInfo = companyService.getCarInfoById(id);
			if (carInfo != null) {
				carInfo.setCarCode(carCode.trim());
				carInfo.setName(name.trim());
				companyService.updateCarInfo(carInfo);
			}

		}
		ModelMap model = new ModelMap();
		model.addAttribute("success", "true");
		return new ModelAndView("jsonView", model);
	}

	public CarInfo getCarInfoById(int id) {

		return companyService.getCarInfoById(id);
	}

}
